package logic

import (
	"context"
	"delivery/cache"
	"fmt"
	"math/rand"
	"time"

	"delivery/deliveryrpc/deliveryrpc"
	"delivery/deliveryrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SenSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSenSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SenSmsLogic {
	return &SenSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SenSmsLogic) SenSms(in *deliveryrpc.SenSmsRequest) (*deliveryrpc.SenSmsResponse, error) {
	// todo: add your logic here and delete this line
	data, _ := cache.Rdb.Get(cache.Ctx, "SenSms"+in.Account).Int()
	if data > 1 {
		return nil, fmt.Errorf("同一分钟只能发送一次")
	}
	i := rand.Intn(9000) + 1000
	//sme, err := pkg.SenSme(in.Account, strconv.Itoa(i))
	//if err != nil {
	//	return nil, err
	//}
	//if sme.Code != 2 {
	//	return nil, fmt.Errorf(sme.Msg)
	//}
	cache.Rdb.Set(cache.Ctx, "SenSms"+in.Account, i, time.Minute*3)
	cache.Rdb.Incr(cache.Ctx, "SenSms"+in.Account)
	return &deliveryrpc.SenSmsResponse{}, nil
}
